<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickName">
      <el-input v-model="dataForm.nickName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="header">
      <el-input v-model="dataForm.header" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birth">
      <el-input v-model="dataForm.birth" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="所在城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="所在城市"></el-input>
    </el-form-item>
    <el-form-item label="学校" prop="school">
      <el-input v-model="dataForm.school" placeholder="学校"></el-input>
    </el-form-item>
    <el-form-item label="学院" prop="college">
      <el-input v-model="dataForm.college" placeholder="学院"></el-input>
    </el-form-item>
    <el-form-item label="在校工号、学号【老师 则 可填工号】" prop="jobId">
      <el-input v-model="dataForm.jobId" placeholder="在校工号、学号【老师 则 可填工号】"></el-input>
    </el-form-item>
    <el-form-item label="真实姓名" prop="trueName">
      <el-input v-model="dataForm.trueName" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="锁定用户【0正常,1锁定】" prop="isLock">
      <el-input v-model="dataForm.isLock" placeholder="锁定用户【0正常,1锁定】"></el-input>
    </el-form-item>
    <el-form-item label="删除记录【1删除,0正常】" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除记录【1删除,0正常】"></el-input>
    </el-form-item>
    <el-form-item label="积分" prop="integration">
      <el-input v-model="dataForm.integration" placeholder="积分"></el-input>
    </el-form-item>
    <el-form-item label="成长值" prop="growth">
      <el-input v-model="dataForm.growth" placeholder="成长值"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          nickName: '',
          mobile: '',
          email: '',
          header: '',
          gender: '',
          birth: '',
          city: '',
          school: '',
          college: '',
          jobId: '',
          trueName: '',
          isLock: '',
          isDeleted: '',
          integration: '',
          growth: '',
          createTime: ''
        },
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          header: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          birth: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '所在城市不能为空', trigger: 'blur' }
          ],
          school: [
            { required: true, message: '学校不能为空', trigger: 'blur' }
          ],
          college: [
            { required: true, message: '学院不能为空', trigger: 'blur' }
          ],
          jobId: [
            { required: true, message: '在校工号、学号【老师 则 可填工号】不能为空', trigger: 'blur' }
          ],
          trueName: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          isLock: [
            { required: true, message: '锁定用户【0正常,1锁定】不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除记录【1删除,0正常】不能为空', trigger: 'blur' }
          ],
          integration: [
            { required: true, message: '积分不能为空', trigger: 'blur' }
          ],
          growth: [
            { required: true, message: '成长值不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.user.username
                this.dataForm.password = data.user.password
                this.dataForm.nickName = data.user.nickName
                this.dataForm.mobile = data.user.mobile
                this.dataForm.email = data.user.email
                this.dataForm.header = data.user.header
                this.dataForm.gender = data.user.gender
                this.dataForm.birth = data.user.birth
                this.dataForm.city = data.user.city
                this.dataForm.school = data.user.school
                this.dataForm.college = data.user.college
                this.dataForm.jobId = data.user.jobId
                this.dataForm.trueName = data.user.trueName
                this.dataForm.isLock = data.user.isLock
                this.dataForm.isDeleted = data.user.isDeleted
                this.dataForm.integration = data.user.integration
                this.dataForm.growth = data.user.growth
                this.dataForm.createTime = data.user.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'nickName': this.dataForm.nickName,
                'mobile': this.dataForm.mobile,
                'email': this.dataForm.email,
                'header': this.dataForm.header,
                'gender': this.dataForm.gender,
                'birth': this.dataForm.birth,
                'city': this.dataForm.city,
                'school': this.dataForm.school,
                'college': this.dataForm.college,
                'jobId': this.dataForm.jobId,
                'trueName': this.dataForm.trueName,
                'isLock': this.dataForm.isLock,
                'isDeleted': this.dataForm.isDeleted,
                'integration': this.dataForm.integration,
                'growth': this.dataForm.growth,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
